package com.westa.smallshop;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentTransaction;
import android.view.View;
import android.widget.RadioButton;

import com.westa.smallshop.common.BaseActivity;
import com.westa.smallshop.common.BaseFragment;

import java.util.List;

import butterknife.BindViews;
import butterknife.ButterKnife;
import butterknife.OnClick;
import cc.solart.wavesidebar.CityPickerActivity;
import cc.solart.wavesidebar.PickerUtil;
import timber.log.Timber;

public class MainActivity extends BaseActivity {


    private static final String ACTIVE_INDEX_TAG = "active_index_tag";
    @BindViews({R.id.tab_0, R.id.tab_1})
    List<RadioButton> mRadioButtonList;
    private static final int TAB_0 = 0;
    private static final int TAB_1 = 1;

    private int activeIndex = TAB_0;

    public static void startActivity(Activity activity) {

        Intent intent = new Intent(activity, MainActivity.class);
        activity.startActivity(intent);
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        //xg
//        updateNotifyService();

        setContentView(R.layout.activity_main);
        ButterKnife.bind(this);
        init();
    }

    private void init() {
        setHasNavigationIcon(false);
        showFragment(activeIndex);
    }

    @OnClick({R.id.tab_0, R.id.tab_1})
    public void onClick(View view) {

        int index = Integer.valueOf(view.getTag().toString());
        showFragment(index);
    }


    private BaseFragment getFragment(int position) {

//                RepairsFragment repairsFragment = (RepairsFragment) getSupportFragmentManager()
//                        .findFragmentByTag(String.valueOf(position));
//                if (repairsFragment == null) {
//                    repairsFragment = RepairsFragment.newInstance();
//                }
//                if (repairsFragment.getPresenter() == null) {
//                    new RepairsPresenter(repairsFragment,
//                            new RepairRemoteDataSource(getApplicationContext()));
//                }
//                return repairsFragment;
        switch (position) {
            case TAB_0:

                break;
            case TAB_1:
                break;
//            case TAB_2:
//                break;
//            case TAB_3: {
//
//                CityPickerActivity.startActivity(this, 1111);
////                Picker.from(this)
////                        .count(3)
////                        .enableCamera(true)
////                        .setEngine(new GlideEngine()).forResult(111);
//                break;
//
//            }
            default:
                return null;
        }
        return null;
    }

    private void showFragment(int index) {

        Fragment fragment = getFragment(index);
        if (fragment == null) {
            return;
        }

        FragmentTransaction tx = getSupportFragmentManager().beginTransaction();

        hideActiveFragment(tx);
        activeIndex = index;
        mRadioButtonList.get(index).setChecked(true);

        if (fragment.isAdded()) {
            tx.show(fragment);
        } else {
            tx.add(R.id.fragment_tab_content, fragment, String.valueOf(index));
        }
        tx.commit();
    }


    private void hideActiveFragment(FragmentTransaction tx) {

        Fragment fragment = getSupportFragmentManager().findFragmentByTag(String.valueOf(activeIndex));
        if (fragment != null) {
            tx.hide(fragment);
            mRadioButtonList.get(activeIndex).setChecked(false);
        }
    }

    @Override
    protected void onRestoreInstanceStateFromBundle(@NonNull Bundle savedInstanceState) {
        super.onRestoreInstanceStateFromBundle(savedInstanceState);
        savedInstanceState.getInt(ACTIVE_INDEX_TAG, TAB_0);
    }

    @Override
    protected void onSaveInstanceState(Bundle outState) {
        super.onSaveInstanceState(outState);
        outState.putInt(ACTIVE_INDEX_TAG, activeIndex);
    }

    @Override
    public boolean onBackOrNavigationPressed() {

        exitApp();
        return true;
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
    }

//    //xg
//    private void updateNotifyService() {
//        XGPushManager.registerPush(this, UserSPUtils.getUuid(getApplicationContext()), new XGIOperateCallback() {
//            @Override
//            public void onSuccess(Object o, int i) {
//                Timber.e("success  " + o);
//            }
//
//            @Override
//            public void onFail(Object o, int i, String s) {
//                Timber.e("failed " + i + "   " + s);
//            }
//        });
//    }


    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);

        if (requestCode == 1111 && resultCode == RESULT_OK && data != null) {
            Timber.e(PickerUtil.obtainResult(data));
        }
    }
}
